/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Beans;

import DAL.MarksDAL;
import Entities.TblExamination;
import Entities.TblMarks;
import Entities.TblStudent;
import Entities.TblSubject;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 *
 * @author CuongNV
 */
@ManagedBean
@RequestScoped
public class MarksManagedBean extends TblMarks {

    /** Creates a new instance of MarksManagedBean */
    public MarksManagedBean() {
    }
    private List<TblMarks> listMarksOfStudent;
    private static TblMarks markBean;
    private float markEdited;

    public List<TblMarks> getAll() {
        try {
            return new BLL.MarksBLL().getAll();
        } catch (Exception ex) {
            Logger.getLogger(BranchManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public String add() {
        try {
            new BLL.MarksBLL().add(this.markBean);
            return "branchlist.xhtml";
        } catch (Exception ex) {
            Logger.getLogger(BranchManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            return "null";
        }
    }

    public String evaluation(float mark) {
        String eval = "";
        if (mark < 10) {
            eval = "False";
        } else if (mark>=10 && mark <= 15) {
            eval = "Pass";
        } else if (mark > 15 && mark < 19) {
            eval = "Good";
        } else if (mark >= 19) {
            eval = "Very Good";
        }

        return eval;
    }

    public String percentage(float mark) {
        float tyLe = mark*100/25;
        return String.valueOf(tyLe) + "%";
    }

    public String layMarksByStudentRollNo(String studentRollNo) {
        listMarksOfStudent = new DAL.MarksDAL().getMarksByStudentRollNo(studentRollNo);
        return "markDetails";
    }
    public TblExamination objExamById(int id){
        return new BLL.ExaminationBLL().getById(id);
    }
    public TblSubject objSubjectById(int id){
        return new BLL.SubjectBLL().getById(id);
    }
    public TblMarks getMarkBean() {
        return markBean;
    }
    public String ConverStatus(int status){
        String result = null;
        if (status==0) {
            result= "re-evaluation";
        }
        if(status==1)
        {
            result= "non approve";
        }
        if(status==2){
            result= "approved";
        }
        return result;
    }
    public void setMarkBean(TblMarks markBean) {
        this.markBean = markBean;
    }

    public List<TblMarks> getListMarksOfStudent() {
        return listMarksOfStudent;
    }

    public void setListMarksOfStudent(List<TblMarks> listMarksOfStudent) {
        this.listMarksOfStudent = listMarksOfStudent;
    }

    public List<TblMarks> layListStudentMarksByExamId(int ExamId){
        return new DAL.MarksDAL().getMarksByExamId(ExamId);
    }

    public List<TblMarks> layListStudentMarks(int classId, int ExamId){
        return new DAL.MarksDAL().getMarksByClassIdAndExamId(classId, ExamId);
    }

    public List<TblStudent> layListStudentByClassId(int classId){
        return new DAL.StudentDAL().getStudentByClassId(classId);
    }


    public void addMark(int examId, int teacherId, String studentRollNo, float mark){
        TblMarks objMark = new TblMarks();
        objMark.setExamId(examId);
        objMark.setTeacherId(teacherId);
        objMark.setStudentRollNo(studentRollNo);
        objMark.setReTest(0);
        objMark.setMark(mark);
        objMark.setRemark("null");

        MarksDAL markDAL = new MarksDAL();
        markDAL.add(objMark);
    }

    public String addStudentMarkForm(int classId, int examId, int teacherId){
        //Check status...
        List<TblStudent> student;
        student = layListStudentByClassId(classId);
        for (int i= 0; i < student.size(); i++) {
            addMark(examId, teacherId, student.get(i).getStudentRollNo(), 0);
        }

        return "results";
    }

    public String editStudentMarkForm(int markId, float mark){
        TblMarks marks = new TblMarks();
        marks.setMarkId(markId);
        marks.setMark(markEdited);
        
        new DAL.MarksDAL().updateSimple(marks);
        return "results";
    }


    public float getMarkEdited() {
        return markEdited;
    }

    public void setMarkEdited(float markEdited) {
        this.markEdited = markEdited;
    }

}
